//
//  BEExpressibleLiteralExtension.swift
//  BasicExtensions
//
//  Created by Fanxx on 2019/5/22.
//  Copyright © 2019 Fanxx. All rights reserved.
//

import UIKit

/// 可以直接赋值给Date，如let date: Date = "2022-10-01 12:00:00"; let date: Date = "2022-10-01"; let date: Date = "12:00:00";
extension Date: ExpressibleByStringLiteral {
    ///使用"2020-06-06 12:00:00"、"2020-06-06"、"12:00:00"的方式初始化
    public init(stringLiteral value: String) {
        let dateformatter = DateFormatter()
        switch value.count {
        case 19: dateformatter.dateFormat = "YYYY-MM-dd HH:mm:ss"
        case 10: dateformatter.dateFormat = "YYYY-MM-dd"
        case 8: dateformatter.dateFormat = "HH:mm:ss"
        default:
            preconditionFailure("This date: \(value) is not invalid")
        }
        guard let date = dateformatter.date(from: value) else {
            preconditionFailure("This date: \(value) is not invalid")
        }
        self = date
    }
}
/// 可以直接赋值给URL，如let url: URL = "http://about:blank";
extension URL: ExpressibleByStringLiteral {
    public init(stringLiteral value: String) {
        guard let url = URL(string: "\(value)") else {
            preconditionFailure("This url: \(value) is not invalid")
        }
        self = url
    }
}
/// 可以直接赋值给URLRequest，如let request: URLRequest = "http://about:blank";
extension URLRequest: ExpressibleByStringLiteral {
    public init(stringLiteral value: String) {
        guard let url = URL(string: "\(value)") else {
            preconditionFailure("This url: \(value) is not invalid")
        }
        self = .init(url: url)
    }
}
/// 可以直接赋值给CGPoint，如let point: CGPoint = 5; let point: CGPoint = [5, 10];
extension CGPoint: ExpressibleByIntegerLiteral, ExpressibleByFloatLiteral, ExpressibleByArrayLiteral {
    public typealias ArrayLiteralElement = CGFloat
    public init(integerLiteral value: IntegerLiteralType) {
        self.init(value)
    }
    public init(floatLiteral value: FloatLiteralType) {
        self.init(value)
    }
    public init(arrayLiteral elements: CGFloat...) {
        self.init(elements[0], elements[1])
    }
}
/// 可以直接赋值给CGSize，如let size: CGSize = 5; let size: CGSize = [5, 10];
extension CGSize: ExpressibleByIntegerLiteral, ExpressibleByFloatLiteral, ExpressibleByArrayLiteral {
    public typealias ArrayLiteralElement = CGFloat
    
    public init(integerLiteral value: IntegerLiteralType) {
        self.init(value)
    }
    public init(floatLiteral value: FloatLiteralType) {
        self.init(value)
    }
    public init(arrayLiteral elements: CGFloat...) {
        self.init(elements[0], elements[1])
    }
}
/// 可以直接赋值给CGRect，如let rect: CGRect = [5, 10]; let rect: CGRect = [1, 2, 5, 10];
extension CGRect: ExpressibleByArrayLiteral {
    public typealias ArrayLiteralElement = CGFloat
    public init(arrayLiteral elements: CGFloat...) {
        if elements.count == 2 {
            self.init(x: 0, y: 0, width: elements[0], height: elements[1])
        } else {
            self.init(x: elements[0], y: elements[1], width: elements[2], height: elements[3])
        }
    }
}
/// 可以直接赋值给UIEdgeInsets，如let insets: UIEdgeInsets = 5; let insets: UIEdgeInsets = [5, 10]; let insets: UIEdgeInsets = [1, 2, 5, 10];
extension UIEdgeInsets: ExpressibleByIntegerLiteral, ExpressibleByFloatLiteral, ExpressibleByArrayLiteral {
    public typealias ArrayLiteralElement = CGFloat
    public init(integerLiteral value: IntegerLiteralType) {
        self.init(CGFloat(value))
    }
    public init(floatLiteral value: FloatLiteralType) {
        self.init(CGFloat(value))
    }
    public init(arrayLiteral elements: CGFloat...) {
        if elements.count == 2 {
            self.init(top: elements[1], left: elements[0], bottom: elements[1], right: elements[0])
        } else {
            self.init(top: elements[0], left: elements[1], bottom: elements[2], right: elements[3])
        }
    }
}

/// GCD延时操作时可以直接传数值，不用写.now() + xxx
extension DispatchTime: ExpressibleByIntegerLiteral, ExpressibleByFloatLiteral {
    public init(integerLiteral value: IntegerLiteralType) {
        self = .now() + .seconds(value)
    }
    public init(floatLiteral value: FloatLiteralType) {
        self = .now() + value
    }
}
//extension UIImage: ExpressibleByStringLiteral {
//    public convenience init(stringLiteral value: String) {
//        self = .init(named: value)!
//    }
//}

//extension UIColor: ExpressibleByIntegerLiteral {
//    public convenience init(integerLiteral value: Int) {
//        self.init(rgb: UInt32(value))
//    }
//}
//
//extension NSError: ExpressibleByStringLiteral {
//    public convenience init(stringLiteral value: String) {
//        self.init(domain: "", code: 0, userInfo: nil)
//    }
//}

/// 可空的字符串ID
public enum BENilableStringIdentifier: Hashable, ExpressibleByStringLiteral, ExpressibleByNilLiteral {
    case `default`
    case value(String)
    public init(nilLiteral: ()) {
        self = .default
    }
    public init(stringLiteral: String) {
        self = .value(stringLiteral)
    }
}
